package com.sf.rbac.web.controller;

import com.github.pagehelper.PageInfo;
import com.sf.rbac.anno.RequiredPermission;
import com.sf.rbac.domain.Employee;
import com.sf.rbac.qo.QueryObject;
import com.sf.rbac.service.IEmployeeService;
import com.sf.rbac.util.JsonResult;
import com.sf.rbac.util.PageResult;
import com.sf.rbac.vo.AdminVo;
import com.sf.rbac.vo.EmployeeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @GetMapping("/list")
    @RequiredPermission(name = "员工列表",expression = "employee:list")
    public JsonResult list(QueryObject queryObject) {
        //PageInfo 帮我们封装分页数据到pageInfo中  pageInfo是pageHelper的对象
        PageInfo<Employee> pageInfo = employeeService.query(queryObject);
        // PageResult 中有想要返回的数据  创建一个PageResult对象
        PageResult result = new PageResult();
        // 将pageInfo中封装的数据传给result中 返回给前端
        BeanUtils.copyProperties(pageInfo, result);
        return JsonResult.success(result);
    }

    @DeleteMapping("/delete/{id}")
    @RequiredPermission(name = "员工删除",expression = "employee:delete")
    public JsonResult delete(@PathVariable Long id) {
        employeeService.delete(id);
        return JsonResult.success();
    }

    @GetMapping("/info/{id}")
    @RequiredPermission(name = "查询单个员工",expression = "employee:info")
    public JsonResult info(@PathVariable Long id) {
        Employee employee = employeeService.get(id);
        return JsonResult.success(employee);
    }

    @PostMapping("/saveOrUpdate")
    @RequiredPermission(name = "员工新增或编辑",expression = "employee:saveOrUpdate")
    public JsonResult saveOrUpdate(@RequestBody EmployeeVo employeeVo) {
        employeeService.saveOrUpdate(employeeVo);
        return JsonResult.success();
    }

    @PostMapping("/updateState")
    @RequiredPermission(name = "更新管理员状态",expression = "employee:updateState")
    public JsonResult updateState(@RequestBody AdminVo adminVo) {
        employeeService.updateState(adminVo);
        return JsonResult.success();
    }
}
